﻿
//全局共用方法对象 add by 不必 20080911 

StringBuffer.prototype.append = function(strValue){this.__strings__.push(strValue);};
StringBuffer.prototype.toString = function(){return this.__strings__.join('');};
function StringBuffer(){this.__strings__ = [];}; 
String.prototype.Trim = function() 
{ 
    var str = this, 
    str = str.replace(/^\s\s*/, ''),  
    ws = /\s/,  
    i = str.length;   
    while (ws.test(str.charAt(--i)));   
    return str.slice(0, i + 1); 
} 
var Utils = new Object();

Utils = 
{
    IsIE:("\v"=="v"),
	GetObj:function(obj) //获取控件
	{
		if(typeof obj == "object"){
			return obj;
		}else {
			if(document.getElementById)
			{
				return document.getElementById(obj);
			}
			else if(document.all)
			{
				return document.all[obj];
			}
			else if(document.layers)
			{
				return document.layers[obj];
			}
		}
	},
	doZoom:function(size,obj) //设置字体
	{
		Utils.GetObj(obj).style.fontSize=size+'px'
	},
	AddFav:function(title,url) //加入收藏夹
	{
		if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function"))//Gecko
		{
			window.sidebar.addPanel(title,url,"");
		}
		else//IE
		{
			window.external.AddFavorite(url,title);
		}
	},
	SetHomepage:function (url) //设为首页
    {
        if (Utils.IsIE){
            document.body.style.behavior='url(#default#homepage)';
            document.body.setHomePage(url);
        }else if (window.sidebar){
            if(window.netscape){
                 try{
                      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                 }catch (e){
                       alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );
                 }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage',url);}
    },
	InsertHtml:function(obj,str) //插入内容
	{
	    if(Utils.GetObj(obj)) Utils.GetObj(obj).innerHTML=str;
	},
	CopyToClip:function(Str,Alert){ //剪帖板
      if(window.clipboardData) {   
              window.clipboardData.clearData();   
              window.clipboardData.setData("Text", Str);   
      } else if(navigator.userAgent.indexOf("Opera") != -1) {   
           window.location = txt;   
      } else if (window.netscape) {   
           try {   
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
           } catch (e) {   
                alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");   
           }   
           var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
           if (!clip)   
                return;   
           var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
           if (!trans)   
                return;   
           trans.addDataFlavor('text/unicode');   
           var str = new Object();   
           var len = new Object();   
           var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
           var copytext = Str;   
           str.data = copytext;   
           trans.setTransferData("text/unicode",str,copytext.length*2);   
           var clipid = Components.interfaces.nsIClipboard;   
           if (!clip)   
                return false;   
           clip.setData(trans,null,clipid.kGlobalClipboard);   
      } 
     if(Alert !=null) alert(Alert); 
	},
	getPageSize:function(){ //内容区实际高度
        var re = {};
        if (document.documentElement && document.documentElement.clientHeight)
        {
            var doc = document.documentElement;
            re.width = (doc.clientWidth>doc.scrollWidth)?doc.clientWidth-1:doc.scrollWidth;
            re.height = (doc.clientHeight>doc.scrollHeight)?doc.clientHeight:doc.scrollHeight;
        }
        else
        {
            var doc = document.body;
            re.width = (window.innerWidth>doc.scrollWidth)?window.innerWidth:doc.scrollWidth;
            re.height = (window.innerHeight>doc.scrollHeight)?window.innerHeight:doc.scrollHeight;
        }
        return re;
    },
    Opacity:function(Obj,value){ //读取或设置透明度
        var ret; 
        if (value==null){   //读取 
            if (!Utils.IsIE) 
                if (ret=Obj.style.opacity) return parseFloat(ret);           
                try{return Obj.filters.item('alpha').opacity/100}catch(x){return 1.0} 
        }else{                   //设置         
             value=Math.min(Math.max(value,0.00001),0.999999)    //这句修复一些非ie浏览器opacity不能设置为1的bug 
              if (Utils.IsIE) Obj.style.filter="alpha(opacity="+value*100+")";
              else Obj.style.opacity=value         
        }        
    },
    GetStyle:function(Obj, style) //获取外部样式值 style:样式名称
    {  
        if(!Utils.IsIE){
          style = style.replace(/\-(\w)/g, function(all, letter){
            return letter.toUpperCase();
          });
          var value = el.currentStyle[style];
          (value == "auto")&&(value = "0px" ); //如果为auto则是0px;
          return value;
        }else{
          return document.defaultView.getComputedStyle(el, null).getPropertyValue(style)   // 这是w3c标准方法，取得元素的样式信息，因为有些样式是在外部css文件定义的，所以用ele.style是取不到的 如果是ie,可以用 ele.currentStyle["name"] 
        }
      },
	//设置选择框默认选择
	GetDefaultItem: function(ItemObj,ItemValue)
	{
		for(i=0;i<ItemObj.options.length;i++)
		{
			if(ItemObj.options[i].value==ItemValue)
			{
				ItemObj.options[i].selected=true
				break
			}
		}
	},
	GoToPage:function(MaxPage,Url){ //为分页控件提供js方法
		if (Utils.GetObj("Page").value!=="") {
			if (Utils.GetObj("Page").value.match(/[^0-9]/)) {
				alert("页数必须是整数,请重新输入！"); 
				Utils.GetObj("Page").value="";
				Utils.GetObj("Page").focus();
				return  false; 
			}else{
				if (Utils.GetObj("Page").value>MaxPage) {
					alert("输入的页数不能大于总页数！"); 
					Utils.GetObj("Page").value="";
					Utils.GetObj("Page").focus();
				}else window.location = Url + Utils.GetObj("Page").value;
			}
		}
	},
    //得到控件的绝对(顶部位置,左边位置,宽度,高度)
    Offset:function(e)
    {
        if (e==null) e=Utils.EventUtil.getObj();
	    var t = e.offsetTop;
	    var l = e.offsetLeft;
	    var w = e.offsetWidth;
	    var h = e.offsetHeight-2;
		
	    while(e=e.offsetParent)
	    {
		    t+=e.offsetTop;
		    l+=e.offsetLeft;
	    }
	    return {
		    top : t,
		    left : l,
		    width : w,
		    height : h
	    }
    },
    //事件处理类,所有相关的方法都要集中到这里*/
     EventUtil: {
        getEvent:function(){ //多浏览器鼠标事件
            if(Utils.IsIE)    return window.event;        
            func=Utils.EventUtil.getEvent.caller;            
            while(func!=null){    
                var arg0=func.arguments[0];
                if(arg0){
                    if((arg0.constructor==Event || arg0.constructor ==MouseEvent)|| (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
                        return arg0;
                    }
                }
                func=func.caller;
            }
            return null;
        },
        getObj:function(){ //多浏览器鼠标事件
            var obj;
            if (Utils.IsIE)obj=Utils.EventUtil.getEvent().srcElement;
	        else obj=Utils.EventUtil.getEvent().target;
	        return obj;
        },
        addEvent:function(obj, type, func){//添加事件
            if (obj.addEventListener)obj.addEventListener(type, func, true);
            else if (obj.attachEvent)obj.attachEvent("on" + type, func);
            else obj["on" + type] = func;
        },
        removeEvent:function(obj, type, func){//移除事件
            
            if (obj.removeEventListener){
                obj.removeEventListener(type, func, true);
            }
            else if (obj.detachEvent)obj.detachEvent("on" + type, func);
            else delete obj["on" + type];
        }
     }
}   

//检查数据格式对象

var C = new Object();
C = 
{
	//函数作用：判断对象的值是否为空(true: 为空)
	isBlank:function(obj)
	{ 
		var strValue 	= obj;
		if(typeof obj != "object")
			obj 		= Utils.GetObj(obj);
		if((typeof obj == "object") && obj != null)
			strValue	= obj.value;
		if(Utils.IsIE)
			var reg 		= /^[\s　]*$/ig;
		else
			var reg 		= /^[\s　]*$/i;
		if(reg.test(strValue) == true) {
			return true;
		}
		return false;
	},

	//函数作用：检索是否含有汉字(true: 不包含; false: 包含)

	checkWord:function(obj)
	{ 
		if(typeof obj != 'object')
			obj = Utils.GetObj(obj);
		var strValue = obj.value;
		if(Utils.IsIE)
			var reg = new RegExp('[\u4e00-\u9fa5]+', 'g');
		else
			var reg = new RegExp('[\u4e00-\u9fa5]+');
		if(reg.test(strValue) == false) { 
			return false; 
		}
		return true; 
	},
	
	//函数作用：检索Email格式是否有效

	checkMail:function(obj) 
	{ 
		if(typeof obj != 'object') 
			obj 		= Utils.GetObj(obj);
		var strValue 	= obj.value;
		if(Utils.IsIE)
			var reg 		= /^\s*\w+([-+.]\w+)*@\w+([-.]\w+)*(\.\w+([-.]\w+)*){1,2}\s*$/gi;
		else
			var reg 		= /^\s*\w+([-+.]\w+)*@\w+([-.]\w+)*(\.\w+([-.]\w+)*){1,2}\s*$/i;
		return reg.test(strValue);;
	},

	//函数作用：检索用户名是否由[a-zA-Z0-9_]的字符组成

	checkAccount:function(obj) 
	{
		if(typeof obj != 'object') 
			obj 		= Utils.GetObj(obj);
		var strValue 		= obj.value;
		if(Utils.IsIE)
			var reg 		= /^[\w]{6,20}$/gi;
		else
			var reg 		= /^[\w]{6,20}$/i;
		if(reg.test(strValue) == false) { 
			return false;
		}
		return true;
	},
	//函数作用：检索密码是否有效(空格以外的所有字符均有效)
	checkPwd:function(obj) 
	{
		if(typeof obj != 'object') 
			obj 		= Utils.GetObj(obj);
		var strValue 		= obj.value;
		if(Utils.IsIE)
			var reg 		= /^\S{6,20}$/gi;
		else
			var reg 		= /^\S{6,20}$/i;
		if(reg.test(strValue) == false) { 
			return false;
		}
		return true;
	},

	//函数作用：检索电话号码格式是否有效
	checkPhone:function(obj) 

	{ 
		if(typeof obj != 'object') 
			obj 		= Utils.GetObj(obj);
		var strValue 		= obj.value;
		if(Utils.IsIE)
		{
			var reg = /^([\d\uFF10-\uFF19]{2,5}[-\uFF0D])?[\d\uFF10-\uFF19]{3,5}[-\uFF0D][1-9\uFF11-\uFF19]{1}[\d\uFF10-\uFF19]{5,7}([-\uFF0D][1-9\uFF11-\uFF19]{1}[\d\uFF10-\uFF19]{1,3})?$/g;
			var reg2 = /^[\d\uFF10-\uFF19]{8,12}$/g;	
		}
		else
		{
			var reg = /^([\d\uFF10-\uFF19]{2,5}[-\uFF0D])?[\d\uFF10-\uFF19]{3,5}[-\uFF0D][1-9\uFF11-\uFF19]{1}[\d\uFF10-\uFF19]{5,7}([-\uFF0D][1-9\uFF11-\uFF19]{1}[\d\uFF10-\uFF19]{1,3})?$/;
			var reg2 = /^[\d\uFF10-\uFF19]{8,12}$/;
		}
		return reg.test(strValue) || reg2.test(strValue);
	},
	checkMobile:function(obj)
	{
		if(obj.value.length > 0)
		{
			if(!C.checkNumber(obj))
			{
				return false;
			}
			var paten =  /^(013|015|13|15|18)\d{9}$/;
			var mobilenum = obj.value;
			if(!paten.exec(mobilenum)){
				return false;
			}
		}
		return true;
	},
	
	//函数作用：检索是否有效的数字
	checkNumber:function(obj) 
	{ 
		if(typeof obj != 'object') 
			obj 		= Utils.GetObj(obj);
		var strValue 	= obj.value;
		var reg 		= new RegExp("^\\d+$");
		if(reg.test(strValue) == false) { 
			return false;
		}
		return true; 
	},
	
	//函数作用：检索url地址是否有效
	checkUrl:function(obj)
	{ 
		if(typeof obj != 'object') 
			obj 		= Utils.GetObj(obj);
		var strValue 	= obj.value;
		if(Utils.IsIE)
			var reg 		= /^\s*(\w{3,6}:\/\/)?([\w-]+(\.[\w-]+)+(\/[\w-.\/\?%&=\u4e00-\u9fa5]*)?)?\s*$/gi;
		else
			var reg 		= /^\s*(\w{3,6}:\/\/)?([\w-]+(\.[\w-]+)+(\/[\w-.\/\?%&=\u4e00-\u9fa5]*)?)?\s*$/i;
		return reg.test(strValue);
	},
	
	//函数作用: 				计算用户输入的字符数
	computeChar:function(obj) 
	{ 
		if(typeof obj != 'object') 
			obj			= Utils.GetObj(obj); 
		var strValue	= obj.value; 
		strValue		= strValue.replace(/^\s+|\s+$/gi, '');
		obj.value	= strValue;
		return obj.value.length;
	},
	radioChecked:function(obj)
	{
	    radios=document.getElementsByName(obj);
		var IsChecked =false;
		for(var i = 0;i<radios.length;i++)

		{
			if(radios[i].checked)

			{
				IsChecked=true;
				break;
			}
		}
		return IsChecked;
	}
}

//Cookie设置读取封装类
var Cookie = new Object();
Cookie = 
{
    //讯取Cookie主键内容
    GetCookie:function(key) 
    { 
        var cookie = document.cookie; 
        var cookieArray = cookie.split(';'); 
        var getvalue = ""; 
        for(var i = 0;i<cookieArray.length;i++) 
        {  
            if(cookieArray[i].Trim().substr(0,key.length) == key) 
            { 
                getvalue = cookieArray[i].Trim().substr(key.length + 1); 
                break; 
            } 
        }
        return getvalue; 
    },
    //读取Cookie子键内容 (主键,子键)
    GetChild:function(cookiekey,childkey) 
    { 
        var child = this.GetCookie(cookiekey); 
        var childs = child.split('&'); 
        var getvalue = "";  
        for(var i = 0;i < childs.length;i++) 
        { 
            if(childs[i].Trim().substr(0,childkey.length) == childkey) 
            { 
                getvalue = childs[i].Trim().substr(childkey.length + 1); 
                break; 
            } 
        } 
        if(getvalue !="")getvalue=decodeURI(getvalue);
    return getvalue; 
    },
    //设置Cookie(主键,值,过期时间,域,路径 
    SetCookie:function(key,value,expire,domain,path) { 
        var cookie = ""; 
        if(key != null && value != null) 
            cookie += key + "=" + value + ";"; 
        if(expire != null) 
            cookie += "expires=" + expire.toGMTString() + ";"; 
        if(domain != null) 
            cookie += "domain=" + domain + ";"; 
        if(path != null) 
            cookie += "path=" + path + ";"; 
        document.cookie = cookie; 
    },
    //删除cookie
    Expire:function(key) 
    { 
        expire_time = new Date(); 
        expire_time.setFullYear(expire_time.getFullYear() - 100); 
        var cookie = " " + key + "=e;expires=" + expire_time + ";" 
        document.cookie = cookie; 
    } 
}